Ext.define('DCApp.view.EMap', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.emap',
    requires: [
       'Ext.ux.WebSocket', 'Ext.ux.WebSocketManager'
    ],
    listeners:{
    	destroy:function(panel, eOpts ){
    		panel.socket.close();    		
    	}
    },
    layout: 'border',
	items: [
        {
            title: 'Footer',
            region: 'south',
            xtype:'eventlist',
            height: 150,
			split: true
        },        
        {
            title: 'Main Content',
            collapsible: false,
            region: 'center',
            margin: '5 0 0 0',
            html: '<h2>Main Page</h2><p>This is where the main content would go</p>'
        }],
    initComponent: function(){
    	var me=this;
    	me.callParent();
    	var websocket = Ext.create ('Ext.ux.WebSocket', {
    		communicationType:'text',
    	    url: DCApp.Cfg.getWSURL(),
    	    listeners: {
    	        open: function (ws) {
    	            //console.log ('The websocket opened');
    	            var sinf = {
    	            	action:'action.subscribe',
    	            	data:[['net.sk.ws.Event1',''],['cn2','e.p2.idnexOf("p")==0']]
    	            	//data:[['net.sk.ws.Event1','e.p1==1'],['cn2','e.p2.idnexOf("p")==0']]
    	            };
    	            ws.send (Ext.encode(sinf));
    	        },
    	        close: function (ws) {
    	            //console.log ('The websocket is closed!');
    	        } ,
    	        message: function (ws, message) {    	            
    	            var rinf=Ext.JSON.decode(message.data);
    	            //console.log ('recv:'+Ext.encode(rinf));
    	            if(rinf.data){
    	            	var grid = me.down('eventlist');
    	            	grid.stopAni();
    	            	var gv = grid.getView();
    	            	
    	            	//grid.getView().getEl().scroll(0, 50,true);
        	            var st =  Ext.data.StoreManager.lookup('st_event1');    
        	            var data = rinf.data;
        	            var recs=[];
        	            for(var i=0; i<data.length; i++){
        	            	var rec = DCApp.model.Event1.create(rinf.data[i]);
        	            	recs.push(rec);       	            	
        	            }
        	            st.insert(0,recs);
        	            grid.startAni();
        	            //grid.updateLayout();
    	            }
    	        }
    	    }
    	});

    	// A 'stop' event is sent from the server
    	// 'data' has 'cmd' and 'msg' fields
    	websocket.on ('stop', function (data) {
    	    //console.log ('Command: ' + data.cmd);
    	    //console.log ('Message: ' + data.msg);
    	});  
    	me.socket = websocket;
    }
});